package com.selft;

import org.elasticsearch.client.Client;

/**
 * Created by xi on 2016/12/7.
 */
public class Test {

    /**
     * Exception in thread "main" [fxt_web/tZEjU02OSwiMA2vNY1OElA] ResourceAlreadyExistsException[index [fxt_web/tZEjU02OSwiMA2vNY1OElA] already exists]
     * 索引存在会报这个异常的
     *
     * @param client
     * @param indexName
     * @param typeName
     * @throws Exception
     */
    public static void initData( final Client client,final String indexName,String typeName)throws  Exception{
        //IndexAndMapping.delIndex(client,indexName);
        //1创建索引和数据结构（表）
        for(int i =0;i<10;i++){
             new Thread(new Runnable() {
                 @Override
                 public void run() {
                     try {
                         IndexAndMapping.createIndex(client, indexName);
                         System.out.println("success thread:"+Thread.currentThread().getId());
                     } catch (Exception e) {
                         System.out.println("error thread:"+Thread.currentThread().getName());
                         e.printStackTrace();
                     }
                 }
             }).start();
        }

        //IndexAndMapping.createIndex(client,indexName);
        long start = System.currentTimeMillis();
        //IndexAndMapping.createMapping(client,indexName,typeName);
        System.out.println("create mapping cost time="+(System.currentTimeMillis()-start)+",ms");
    }

    public static void addOrUpdateData(Client client,String indexName,String typeName){
        //2添加数据
        OpManager.singleAddDoc(client, indexName, typeName, "1", IndexAndMapping.initSingleDocument());
    }

    //多线程去创建会报错吗？


    public static void main(String[] args)throws  Exception{
       final Client client = EsClientFacotory.getInstance();
        //创建索引
       final String indexName="fxt_web";
        final   String typeName ="xiaocai";
        initData(client,indexName,typeName);



//        System.out.println(OpManager.getOneDoc(client, indexName, typeName, "1"));
        try{
            //IndexAndMapping.createMapping2(client,indexName,typeName);
            //测试下Must 和should
            //addOrUpdateData(client, indexName, typeName);

//           OpManager opManager =  new OpManager();
//            Map<String,Object> map = opManager.getSingleRecordByPrimaryKey(client,indexName,typeName,"1");
//           IndexAndMapping.createIndexWithRest(EsClientFacotory.getRestClient(),"fxt_web_rest2");
//

            OpManager.searchBySelfClasss(client, indexName, typeName);
            OpManager.searchNullValue(client, "fxtweb_ruiyang3", "property");
            OpManager.searchOrder(client, "fxtweb_ruiyang3", "property");
        }catch (Exception e){
            e.printStackTrace();
        }

       // client.close();


    }

}
